package com.bjsxt.web.servlet;

import com.bjsxt.commons.Constants;
import com.bjsxt.exception.UserNotFoundException;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserLoginService;
import com.bjsxt.service.impl.UserLoginServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/*处理用户请求*/
@WebServlet("/login.do")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //在登录的表单中取到 usernmae userpwd
        //将取到的数据 放进业务层  实例化业务层  然后Jdbc 获得用户
        String username = req.getParameter("username");
        String userpwd = req.getParameter("userpwd");
        //获取用户在页面输入的验证码
        String code = req.getParameter("code");
        try{
            //获得HttpSession 对象
            HttpSession session = req.getSession();

            //判断验证码是否正确   用户在页面输入的验证码与存在session的验证码作 比较，相同就可以登录
            //获取存在session的验证码
            String codeTemp = (String) session.getAttribute(Constants.VALIDATE_CODE_KEY);
            if(codeTemp.equals(code)) {


                UserLoginService userLoginService = new UserLoginServiceImpl();
                Users users = userLoginService.userLogin(username, userpwd);
                //建立客户端与服务端的会话

                session.setAttribute(Constants.USER_SESSION_KEY, users);

                //用户只能在一处登录  登录后用户信息才存在在servletContext，所以先获得ServletContext对象
                // 根据id是获得session  如果存在则先删除之前的session 再登录
                ServletContext servletContext = this.getServletContext();
                //双引号 是强转为String类型
                HttpSession temp = (HttpSession) servletContext.getAttribute(users.getUserid() + "");
                if (temp != null) {
                    //用户已经在登录  所以删除servletContext 中的session
                    servletContext.removeAttribute(users.getUserid() + "");
                    temp.invalidate();//使session对象无效
                }
                //重新登录 在servletContext存入session 这里是上面的session 不是temp
                servletContext.setAttribute(users.getUserid() + "", session);

                //使用重定向跳转  不可以用请求转发 因为要跳转到另一页面
                resp.sendRedirect("main.jsp");
            }else {
                req.setAttribute(Constants.REQUEST_MSG,"验证码有误，请重新输入");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        }catch (UserNotFoundException e){
            //当登录不到时
            //获得在业务层显示的错误信息
            req.setAttribute("msg",e.getMessage());
            //在同一页面跳转
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        } catch (Exception e){

            resp.sendRedirect("error.jsp");
        }

    }
}
